package cn.edu.nju.ws.sview.wyge.relations.index;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.logging.SimpleFormatter;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.rdf.URIResource;
import cn.edu.nju.ws.sview.views.Collaboration;
import cn.edu.nju.ws.sview.wyge.relations.adataset.comment.DatasetComment;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetName;
import cn.edu.nju.ws.sview.wyge.relations.index.indexTask.IndexTask;

/**
 * index manager
 * 
 * @author Qingxia Liu
 */
public class SearchIndexManager {
	static SearchIndexManager im = null;
	static Directory dir = null;
	private static File file = null;
	public static String FIELDNAME_DS = "ds";
//	String FIELDNAME_TOTAL = "total";
	public static String FIELDNAME_NAME = "name";
	public static String FIELDNAME_DESCRIPTION = "description";
	public static String FIELDNAME_URI = "uri";
	private static IndexWriter writer = null;
	
	static{
		Properties properties = new Properties();
		try {
			properties.load(SearchIndexManager.class.getClassLoader().getResourceAsStream("conf.properties")) ;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		file = new File(properties.getProperty("search_index_directory"));
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,
				new StandardAnalyzer(Version.LUCENE_35));
		try {
			if(dir==null){
				dir = FSDirectory.open(file);
			}
			writer = new IndexWriter(dir, iwc);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// ==============for test
	public static void main(String[] args) throws IOException, SQLException {
//		long start = System.currentTimeMillis();
//		SearchIndexManager.getIndexManager().initialize(null);
//		SearchIndexManager.getIndexManager().closeAll();
//		System.out.println("cost time:"+(System.currentTimeMillis()-start));
//		SearchIndexManager.getIndexManager().showAll(6500,7000);
//		ArrayList<DatasetItem> list = SearchIndexManager.getIndexManager().getDatasetsByNameUri("voca", "en", 15);//.getDatasetsByContent("ontology","en", 20);
//		ArrayList<DatasetItem> list = new ArrayList<DatasetItem>();
//		SearchIndexManager.getIndexManager().getDatasetsByContent("The OWL 2 Schema vocabulary (OWL 2)", "", 0, 10, list);//.getDatasetsByContent("ontology","en", 20);
//		System.out.println("num:"+list.size());
//		for(DatasetItem d : list){
//			System.out.println(d.toString()+"\t"+d.getUri()+"\t"+d.getNames(0, -1).toString());
//		}
		String str = SearchIndexManager.getIndexManager().getHighlightedText("The OWL 2 Schema vocabulary (OWL 2)", "Schema voca");
		System.out.println("hl:"+str);
	}

	public void showAll(int start,  int end) {
		try {
			if (dir == null) {
				dir = FSDirectory.open(file);
			}
			IndexReader ir = IndexReader.open(dir);
			Document doc = null;
			System.out.println("total:" + ir.numDocs());
			for (int i = start; i < ir.numDocs() && i<end; i++) {
				doc = ir.document(i);
				System.out.println(i+":"+doc.get(FIELDNAME_DS) + "\t"
						+"|name:"+ doc.get(FIELDNAME_NAME) + "\t"
//						+ doc.get(FIELDNAME_TOTAL) + "\t"
						+"|uri:"+ doc.get(FIELDNAME_URI) + "\t"
						+"|desc:"+ doc.get(FIELDNAME_DESCRIPTION));
			}
			System.out.println("total:" + ir.numDocs());
			ir.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// ===========
	/**
	 * ！！！当对象不需再用时，一定要调用此函数关闭directory
	 */
	public void closeAll() {
		try {
			SearchIndexManager.writer.close();
			if (dir != null) {

				dir.close();

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static SearchIndexManager getIndexManager() {
		if (im != null) {
			return im;
		}
		Properties properties = new Properties();
		try {
			properties.load(SearchIndexManager.class.getClassLoader()
					.getResourceAsStream("conf.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		im = new SearchIndexManager();
		properties.clear();
		return im;
	}

	public void initialize(String from) throws SQLException {
		try {
			if (dir == null) {
				dir = FSDirectory.open(file);
			}
			IndexReader ir = IndexReader.open(dir);
			ir.close();
		} catch (Exception e) {
			ArrayList<DatasetItem> dss = new ArrayList<DatasetItem>();
			new IndexTask(dss);

			Connection con2 = DBConnectionFactory.getConnection();
			Statement stat2 = con2.createStatement();
//			ResultSet rs2 = stat2.executeQuery("SELECT id, name FROM custom_dataset");
			ResultSet rs2 = stat2.executeQuery("SELECT id FROM custom_dataset");
			while (rs2.next()) {
				int id = rs2.getInt(1);
//				String name = rs2.getString(2);
				DatasetItem ds = new DatasetItem("custom", id, null);//, name);
				dss.add(ds);
			}
			rs2.close();
			stat2.close();
			con2.close();

			addDatasets(dss);
			
			addUriDatasets();
			
		}
		
	}
	private void addUriDatasets(){
		ArrayList<DatasetItem> udss = new ArrayList<DatasetItem>();
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("select distinct doc_id from quadruple");
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				int id = rs.getInt(1);
				String uri = URIIDCache.getInstance().getURI(id);
				DatasetItem ds = new DatasetItem("uri", id, uri);
				udss.add(ds);
			}
			rs.close();
			pstmt.close();
			conn.close();
			
			addDatasets(udss);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * 
	 * @param path
	 *            索引所在目录
	 */
	public SearchIndexManager() {
	}

	/**
	 * 通过term检索url
	 * 
	 * @param keyword
	 * @param topnum
	 *            最多返回的结果条数
	 * @return
	 */
	public ArrayList<DatasetItem> getDatasetsByNameUri(String keyword, String lang, int topnum) {
		ArrayList<DatasetItem> dss = new ArrayList<DatasetItem>();
		try {
			if (dir == null) {
				dir = FSDirectory.open(file);
			}
			IndexReader ir = IndexReader.open(dir);
			IndexSearcher is = new IndexSearcher(ir);

			StringSplitter split = StringSplitter.getStringSplitter();
			List<String> kws = split.splitByDelimit(keyword);
			
			BooleanQuery query = new BooleanQuery();
			
			BooleanQuery bq_name = new BooleanQuery();
			BooleanQuery bq_uri = new BooleanQuery();
//			System.out.println("kws:"+kws.toString());
			for (String kw : kws) {
				kw = kw.toLowerCase();
				if (kw.equals("http") || kw.equals("ftp")) {
					continue;
				}
				Query tq_name_text = new PrefixQuery(new Term(FIELDNAME_NAME, kw));
				Query tq_uri = new PrefixQuery(new Term(FIELDNAME_URI, kw));
				
				bq_name.add(tq_name_text, Occur.MUST);
				bq_uri.add(tq_uri, Occur.MUST);
				
//				bq_name.add(tq_name, Occur.MUST);
//				bq_desc.add(tq_desc, Occur.MUST);
//				bq_uri.add(tq_uri, Occur.MUST);
				
			}
			if(lang!=null && lang.trim().length()>0){
				Query tq_name_lang = new TermQuery(new Term(FIELDNAME_NAME, lang));
				bq_name.add(tq_name_lang, Occur.MUST);//只能搜索当前语言下的内容
			}
			
			query.add(bq_name, Occur.SHOULD);
			query.add(bq_uri, Occur.SHOULD);
			
			TopDocs tdoc = is.search(query, topnum);
			ScoreDoc[] docs = tdoc.scoreDocs;
			for (ScoreDoc sd : docs) {
				String ds_serial = is.doc(sd.doc).get(FIELDNAME_DS);
//				String name = is.doc(sd.doc).get(FIELDNAME_NAME);
				String[] names = is.doc(sd.doc).getValues(FIELDNAME_NAME);
				String uri = is.doc(sd.doc).get(FIELDNAME_URI);
				String[] sps = ds_serial.split(":");
				DatasetItem ds = new DatasetItem(sps[0], Integer.parseInt(sps[1]), uri);//name);
				
				if(names.length==0){
					ds.setShowName(uri);
				}else{
					if(ds.getGenre().contentEquals(DatasetItem.URI_GENRE)){
						Explanation explanation = is.explain(bq_uri, sd.doc);
						if(explanation.isMatch()){
							ds.setShowName(uri);
						}else{
							boolean flag = false;
							for(String nam : names){
								if(nam.endsWith("@"+lang)){
									ds.setShowName(nam.substring(0,nam.lastIndexOf("@")));
									flag = true;
									break;
								}
							}
							if(!flag){
								ds.setShowName(names[0]);
							}
						}
					}else{
						Explanation explanation = is.explain(bq_name, sd.doc);
						if(explanation.isMatch()){
							boolean flag = false;
							for(String nam : names){
								if(nam.endsWith("@"+lang)){
									ds.setShowName(nam.substring(0, nam.lastIndexOf("@")));
									flag = true;
									break;
								}
							}
							if(!flag){
//								System.out.println(names[0]);
								ds.setShowName(names[0].substring(0,names[0].lastIndexOf("@")));
							}
						}else{
							ds.setShowName(uri);
						}
					}
				}
//				Explanation explanation = is.explain(bq_uri, sd.doc);
//				if(explanation.isMatch()){
//					ds.setBookmarkName(uri);
//				}else{
//					ds.setBookmarkName();
//				}
//				System.out.println("exp:"+explanation.isMatch());
//				System.out.println((is.doc(sd.doc).getValues(FIELDNAME_NAME).length)+"|"+ds_serial+"|uri:"+uri+"|"+ds.getUri()+"|"+names[0]);//ds.getBookmarkName());
				
//				Explanation[] dt = explanation.getDetails();
//				for(int i=0; i<dt.length; i++){
//					System.out.println("exp:"+dt[i].toString());
//				}
				
				dss.add(ds);
			}
			is.close();
			ir.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dss;
	}
	/**
	 * 
	 * @param keyword
	 * @param lang
	 * @param topnum
	 * @return
	 */
	public int getDatasetsByContent(String keyword, String lang,int start, int length, ArrayList<DatasetItem> result) {
		int topnum = start+length;
		int totalNum = 0;
//		ArrayList<DatasetItem> dss = new ArrayList<DatasetItem>();
		try {
			if (dir == null) {
				dir = FSDirectory.open(file);
			}
			IndexReader ir = IndexReader.open(dir);
			IndexSearcher is = new IndexSearcher(ir);

//			BooleanQuery query = new BooleanQuery();
//			QueryParser qp_name = new QueryParser(Version.LUCENE_35, FIELDNAME_NAME, new StandardAnalyzer(Version.LUCENE_35));
//			Query qn = qp_name.parse("("+keyword+" "+lang+")");
//			
//			query.add(qn, Occur.SHOULD);
//			QueryParser qp_uri = new QueryParser(Version.LUCENE_35, FIELDNAME_URI, new StandardAnalyzer(Version.LUCENE_35));
//			Query qu = qp_uri.parse(keyword);
//			query.add(qu, Occur.SHOULD);
//			QueryParser qp_desc = new QueryParser(Version.LUCENE_35, FIELDNAME_DESCRIPTION, new StandardAnalyzer(Version.LUCENE_35));
//			Query qd = qp_desc.parse(keyword);
//			query.add(qd, Occur.SHOULD);
//			System.out.println("qn:"+(qn.toString().length())+"\nqu:"+qu.toString()+"\nqd:"+qd.toString());
			StringSplitter split = StringSplitter.getStringSplitter();
			List<String> kws = split.splitByDelimit(keyword);
//			System.out.println("kws:"+kws.toString());
			
			BooleanQuery query = new BooleanQuery();
			
			BooleanQuery bq_name = new BooleanQuery();
			bq_name.setBoost((float) 1.5);
			BooleanQuery bq_desc = new BooleanQuery();
			bq_desc.setBoost((float) 0.5);
			BooleanQuery bq_uri = new BooleanQuery();
			bq_uri.setBoost((float) 1);
			
//			QueryParser qp_name = new QueryParser(Version.LUCENE_35, FIELDNAME_NAME, new StandardAnalyzer(Version.LUCENE_35));

//			System.out.println("kws:"+kws.toString());
			for (String kw : kws) {
				kw = kw.toLowerCase();
				if (kw.equals("http") || kw.equals("ftp")) {
					continue;
				}
				Query tq_name_text = new TermQuery(new Term(FIELDNAME_NAME, kw));//new PrefixQuery(new Term(FIELDNAME_NAME, kw));
				Query tq_desc_text = new TermQuery(new Term(FIELDNAME_DESCRIPTION, kw));
				
				
				Query tq_uri = new TermQuery(new Term(FIELDNAME_URI, kw));
				
//				if(qp_name.parse(kw).toString().length()==0){
					bq_name.add(tq_name_text, Occur.SHOULD);
					bq_desc.add(tq_desc_text, Occur.SHOULD);
					bq_uri.add(tq_uri, Occur.SHOULD);
//				}else{
//					bq_name.add(tq_name_text, Occur.MUST);
//					bq_desc.add(tq_desc_text, Occur.MUST);
//					bq_uri.add(tq_uri, Occur.MUST);
//				}
				
				
//				bq_name.add(tq_name, Occur.MUST);
//				bq_desc.add(tq_desc, Occur.MUST);
//				bq_uri.add(tq_uri, Occur.MUST);
				
			}
			if(lang!=null && lang.trim().length()>0){
				Query tq_name_lang = new TermQuery(new Term(FIELDNAME_NAME, lang));
				bq_name.add(tq_name_lang, Occur.SHOULD);
				Query tq_desc_lang = new TermQuery(new Term(FIELDNAME_DESCRIPTION, lang));
				bq_desc.add(tq_desc_lang, Occur.SHOULD);
			}
			
			query.add(bq_name, Occur.SHOULD);
//			System.out.println(query.toString());
			query.add(bq_desc, Occur.SHOULD);
			query.add(bq_uri, Occur.SHOULD);
			
			TopDocs tdoc = is.search(query, topnum);
			totalNum = tdoc.totalHits;
			ScoreDoc[] docs = tdoc.scoreDocs;
			if(docs.length>=start){
				for(int i=start; i<docs.length; i++){
					ScoreDoc sd = docs[i];
					String ds_serial = is.doc(sd.doc).get(FIELDNAME_DS);
//					String name = is.doc(sd.doc).get(FIELDNAME_NAME);
					String[] names = is.doc(sd.doc).getValues(FIELDNAME_NAME);
					String uri = is.doc(sd.doc).get(FIELDNAME_URI);
					String[] sps = ds_serial.split(":");
					DatasetItem ds = new DatasetItem(sps[0], Integer.parseInt(sps[1]), uri);//name);
					
//					System.out.println(sd.doc+"|"+sd.score+":"+ds.toString()+"\t"+ds.getUri()+"\t"+ds.getNames(0, -1).toString()+"/t|desc:"+ds.getDescriptions(0, -1).toString());
					
//tobe completed to include description
//					if(names.length==0){
//						ds.setShowName(uri);
//					}else{
//						if(ds.getGenre().contentEquals(DatasetItem.URI_GENRE)){
//							Explanation exp_uri = is.explain(bq_uri, sd.doc);
//							if(exp_uri.isMatch()){
//								ds.setShowName(uri);
//							}else{
//								boolean flag = false;
//								for(String nam : names){
//									if(nam.endsWith("@"+lang)){
//										ds.setShowName(nam.substring(0,nam.lastIndexOf("@")));
//										flag = true;
//										break;
//									}
//								}
//								if(!flag){
//									ds.setShowName(names[0]);
//								}
//							}
//						}else{
//							Explanation explanation = is.explain(bq_name, sd.doc);
//							if(explanation.isMatch()){
//								boolean flag = false;
//								for(String nam : names){
//									if(nam.endsWith("@"+lang)){
//										ds.setShowName(nam.substring(0, nam.lastIndexOf("@")));
//										flag = true;
//										break;
//									}
//								}
//								if(!flag){
////									System.out.println(names[0]);
//									ds.setShowName(names[0].substring(0,names[0].lastIndexOf("@")));
//								}
//							}else{
//								ds.setShowName(uri);
//							}
//						}
//					}
////					dss.add(ds);
					result.add(ds);
				}
			}
			
			is.close();
			ir.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
//		System.out.println("totalNum:"+totalNum+"|results:"+result.size());
//		return dss;
		return totalNum;
	}
	public static String getHighlightedText(String text, String keyword){
		String result = null;
		StringSplitter split = StringSplitter.getStringSplitter();
		List<String> kws = split.splitByDelimit(keyword);
		BooleanQuery query = new BooleanQuery();
		
		BooleanQuery bq_name = new BooleanQuery();
		bq_name.setBoost((float) 1.5);
		BooleanQuery bq_desc = new BooleanQuery();
		bq_desc.setBoost((float) 0.5);
		BooleanQuery bq_uri = new BooleanQuery();
		bq_uri.setBoost((float) 1);
		
		for (String kw : kws) {
			kw = kw.toLowerCase();
			if (kw.equals("http") || kw.equals("ftp")) {
				continue;
			}
			Query tq_name_text = new TermQuery(new Term(FIELDNAME_NAME, kw));
//			Query tq_desc_text = new PrefixQuery(new Term(FIELDNAME_DESCRIPTION, kw));
			
			
			Query tq_uri = new TermQuery(new Term(FIELDNAME_URI, kw));
			
				bq_name.add(tq_name_text, Occur.SHOULD);
//				bq_desc.add(tq_desc_text, Occur.SHOULD);
//				bq_uri.add(tq_uri, Occur.SHOULD);
		}
		
		
		query.add(bq_name, Occur.SHOULD);
//		System.out.println(query.toString());
//		query.add(bq_desc, Occur.SHOULD);
//		query.add(bq_uri, Occur.SHOULD);
		
		QueryScorer scorer = new QueryScorer(query);
		SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span class='highlight'>","</span>");
		Highlighter highlighter = new Highlighter(formatter, scorer);
		Fragmenter fragmenter = new SimpleFragmenter(50);
		highlighter.setTextFragmenter(fragmenter);
		TokenStream tokenStream = new StandardAnalyzer(Version.LUCENE_35).tokenStream(FIELDNAME_NAME, new StringReader(text));
		try {
			result = highlighter.getBestFragments(tokenStream, text, 5, "...");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidTokenOffsetsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(result==null || result.length()==0){
			result = text;
		}
		return result;
		
		
	}
	/**
	 * 删除一个url
	 * 
	 * @param url
	 */
	public void removeDataset(DatasetItem ds) {
		long start = System.currentTimeMillis();
		try {
			if (dir == null) {
				dir = FSDirectory.open(file);
			}
			IndexWriter iw = this.getWriter(dir);
			String ds_serial = ds.getGenre() + ":" + ds.getId();
			iw.deleteDocuments(new Term(FIELDNAME_DS, ds_serial));
			iw.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("removed! cost time:"
				+ (System.currentTimeMillis() - start));
	}

	public void addDatasets(ArrayList<DatasetItem> dss) {
		IndexWriter iw;
		try {
			if (dir == null) {
				dir = FSDirectory.open(file);
			}
			iw = this.getWriter(dir);
			Document doc = null;
			String ds_serial = null;
			int i;
			for (i = 0; i < dss.size(); i++) {
				DatasetItem ds = dss.get(i);
				ds_serial = ds.getGenre() + ":" + ds.getId();
				doc = new Document();
				doc.add(new Field(FIELDNAME_DS, ds_serial, Store.YES,
						Index.NOT_ANALYZED_NO_NORMS));
				
				doc.add(new Field(FIELDNAME_URI, ds.getUri(), Store.YES, Index.ANALYZED));
				//add names
				ArrayList<DatasetName> names = ds.getNames(0, -1);
//				if(names.size()>1){
//					System.out.println(ds_serial+"|"+names.toString());
//				}
				for(DatasetName dn : names){
					String dnlang = dn.getLang();
					if(dnlang==null || dnlang.trim().length()==0){
						dnlang="en";
					}
					doc.add(new Field(FIELDNAME_NAME, dn.getText()+"@"+dnlang, Store.YES, Index.ANALYZED));//=====
//					doc.add(new Field(FIELDNAME_TOTAL, getSplitedString(ds.getName()), Store.YES, Index.ANALYZED));
				}
				ArrayList<DatasetComment> comments = ds.getDescriptions(0, -1);
				for(DatasetComment dc : comments){
					String dclang = dc.getLang();
					if(dclang==null || dclang.trim().length()==0){
						dclang="en";
					}
					doc.add(new Field(FIELDNAME_DESCRIPTION, dc.getText()+"@"+dclang, Store.YES, Index.ANALYZED));
				}
//				if(ds.getGenre().contentEquals(DatasetItem.CUSTOM_GENRE)){
//					doc.add(new Field(FIELDNAME_NAME, ds.getName(), Store.YES, Index.NOT_ANALYZED_NO_NORMS));
//					doc.add(new Field(FIELDNAME_TOTAL, getSplitedString(ds.getName()), Store.YES, Index.ANALYZED));
//					ArrayList<DatasetComment> comments = DatasetComment.getDescriptions(ds.getGenre(), ds.getId(), 0, -1);
//					for(DatasetComment dc : comments){
//						doc.add(new Field(FIELDNAME_DESCRIPTION, dc.getLang()+" "+dc.getText(), Store.YES, Index.ANALYZED));
//					}
//				}else if(ds.getGenre().contentEquals(DatasetItem.URI_GENRE)){
//					addUriNames(doc, ds.getId());
//				}
				//add description
				
				iw.updateDocument(new Term(FIELDNAME_DS, ds_serial), doc);
			}
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private String getSplitedString(String url) {
		if (url.indexOf("://") != -1) {
			url = url.substring(7);
		}
		String str = StringSplitter.getStringSplitter().splitByDelimit(url)
				.toString();
		str = str.replace(", ", " ");
		str = str.replace("[", "");
		str = str.replace("]", "");
		return str;
	}

	private IndexWriter getWriter(Directory dir) throws CorruptIndexException,
			LockObtainFailedException, IOException {
		return SearchIndexManager.writer;
	}
}
